package com.whw.fy.utils;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class GetEveryDate {
    //获得所在周的每一天 日期格式为：MM/dd
    public static List<String> dateToWeek(Date mdate){

        int b=mdate.getDay();
        if (b==0)
        {
            b=7;
        }
        Date fdate ;
        List <String> list = new ArrayList();
        Long fTime=mdate.getTime()-(b-1)*24*3600000;
        for(int a=0;a<7;a++)
        {
            fdate= new Date();
            fdate.setTime(fTime+(a*24*3600000));
            String dt = new SimpleDateFormat("MM/dd").format(fdate);
            list.add(a, dt);
        }
        return list;
    }

    //获得所在周的每一天 日期格式为：yyyy-MM-dd
    public static List<String> dateToWeekComplete(Date mdate){

        int b=mdate.getDay();
        if (b==0)
        {
            b=7;
        }
        Date fdate ;
        List <String> list = new ArrayList();
        Long fTime=mdate.getTime()-(b-1)*24*3600000;
        for(int a=0;a<7;a++)
        {
            fdate= new Date();
            fdate.setTime(fTime+(a*24*3600000));
            String dt = new SimpleDateFormat("yyyy-MM-dd").format(fdate);
            list.add(a, dt);
        }
        return list;
    }

    //根据数字获得周几
    public static String getWeekDayBynumber(int i){
        String str="";
        switch (i){
            case 1:
                str = "星期一";
                break;
            case 2:
                str = "星期二";
                break;
            case 3:
                str = "星期三";
                break;
            case 4:
                str = "星期四";
                break;
            case 5:
                str = "星期五";
                break;
            case 6:
                str = "星期六";
                break;
            case 7:
                str = "星期日";
                break;
        }
        return str;
    }

    /**
     * 根据日期获取 星期 （2019-05-06 ——> 星期一）
     * @param datetime
     * @return
     */
    public static int GetWeekByDate(String datetime) {

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        Date date;
        try {
            date = f.parse(datetime);
            cal.setTime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 7;
        if (w==0){
            w=7;
        }
        return w;
    }

    public static int GetTimeBetween(Date date1,Date date2){
        return (int)DateUtil.between(date1,date2, DateUnit.DAY);
    }

    /**
     * 获取未来第几天的日期
     *
     * @param after
     * @return
     */
    public static Date getAfterDate(int after,String dateToString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateToString);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + after);
        Date today = calendar.getTime();
        return today;
    }
}
